package com.e3.web.core.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public abstract class SuperQC implements Serializable {
	
	private static final long serialVersionUID = -829111708832911040L;

	private String prefix;
	
	private Long corpId = -1L;

	public String getPrefix() {
		return prefix;
	}

	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}
		
	public Long getCorpId() {
		return corpId;
	}

	public void setCorpId(Long corpId) {
		this.corpId = corpId;
	}

	public String getStringStatement(String name,String value){
		if(value.startsWith("%")||value.endsWith("%")){
			return getLikeStatement(name,value);
		}
		return getStrEqualsStatement(name,value);
	}
	
	public String getLikeStatement(String name,String value){
		return getPrefix()+"."+name+" like '"+value+"'";
	}
	
	public String getNumEqualsStatement(String name,Number value){
		return getPrefix()+"."+name+"="+value;
	}
	
	public String getStrEqualsStatement(String name,String value){
		return getPrefix()+"."+name+"='"+value+"'";
	}
	
	public String getGreaterStatement(String name,Number value){
		return getPrefix()+"."+name+">"+value;
	}
	
	public String getLesserStatement(String name,Number value){
		return getPrefix()+"."+name+"<"+value;
	}
	
	public String getLesserEqStatement(String name,Number value){
		return getPrefix()+"."+name+"<="+value;
	}
	
	public String getGreaterEqStatement(String name,Number value){
		return getPrefix()+"."+name+">="+value;
	}
	
	public String getBetweenClosedStatement(String name,Number lesser,Number greater){
		return getPrefix()+"."+name+">="+lesser+" AND "+getPrefix()+"."+name+"<="+greater;
	}
	
	public String getBetweenOpenStatement(String name,Number lesser,Number greater){
		return getPrefix()+"."+name+">"+lesser+" AND "+getPrefix()+"."+name+"<"+greater;
	}
	
	public String getWhereSQL(){
		List<String> fields = getWhereList();
		Long corpId = getCorpId();
		if(corpId>=0){
			if(fields==null){
				fields = new ArrayList<String>();
			}
			fields.add(getNumEqualsStatement("corpId", corpId));
		}
		return generateWhereSQL(fields);
	}
	
	protected String generateWhereSQL(List<String> fields){
		if(fields==null||fields.isEmpty()){
			return null;
		}
		StringBuffer sql = new StringBuffer();
		int size = fields.size();
		for (int i = 0; i < size; i++) {
			String string = fields.get(i);
			sql.append(string);
			if (i < size - 1) {
				sql.append(" AND ");
			}
		}
		return sql.toString();
	}
	
	public abstract List<String> getWhereList();
}
